package com.whut.mobilesafe.activity;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.whut.mobilesafe.R;
import com.whut.mobilesafe.viewholder.ListItemContractHolder;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 作    者:yubo.xiaoyubo
 * 时    间：2015/12/24 15:33
 * 版    本：1.0
 * 描    述：当step3Activity的选择联系人按钮被点击之后
 */
public class ContractActivity extends Activity {

    private ArrayList<HashMap<String, String>> contractList;
    private ListView lvContact;
    private Adapter mAdapter;
    private ListItemContractHolder mHolder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contract);

        lvContact = (ListView) findViewById(R.id.lv_contact);

        //异步初始化数据
        initData();
        //将用户选择的那个联系人的信息返回给step3Activity
        lvContact.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String phone = contractList.get(position).get("phone");
                Intent intent = new Intent();
                intent.putExtra("phone",phone);
                setResult(0,intent);
                finish();
            }
        });
    }

    private void initData() {

        new Thread() {
            @Override
            public void run() {

                contractList = readContract();

                //在ui线程中更新ListView
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mAdapter = new Adapter();
                        lvContact.setAdapter(mAdapter);
                    }
                });
            }
        }.start();
    }


    class Adapter extends BaseAdapter {
        @Override
        public int getCount() {
            return contractList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                mHolder = new ListItemContractHolder();
                convertView = View.inflate(getApplicationContext(), R.layout.list_item_contract, null);
                convertView.findViewById(R.id.tv_name);
                mHolder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
                mHolder.tvPhone = (TextView) convertView.findViewById(R.id.tv_phone);
                convertView.setTag(mHolder);
            } else {
                mHolder = (ListItemContractHolder) convertView.getTag();
            }
            String name = contractList.get(position).get("name");
            String phone = contractList.get(position).get("phone");
            mHolder.tvName.setText(name);
            mHolder.tvPhone.setText(phone);
            return convertView;
        }
    }

    //读取数据
    public ArrayList<HashMap<String, String>> readContract() {
        Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
        Cursor contractCursor = getContentResolver().query(uri, new String[]{"contact_id"}, null, null, null);
        ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
        while (contractCursor.moveToNext()) {

            String contractID = contractCursor.getString(0);

            Cursor cursor = getContentResolver().query(Uri.parse("content://com.android.contacts/data"),
                    new String[]{"data1", "mimetype"}, "raw_contact_id=?", new String[]{contractID}, null);

            HashMap<String, String> map = new HashMap<String, String>();
            while (cursor.moveToNext()) {

                String data = cursor.getString(0);
                String type = cursor.getString(1);

                if ("vnd.android.cursor.item/phone_v2".equals(type)) {
                    // 电话号码
                    map.put("phone", data);
                } else if ("vnd.android.cursor.item/name".equals(type)) {
                    // 名称
                    map.put("name", data);
                }
            }

            cursor.close();
            //过略脏数据
            if (!TextUtils.isEmpty(map.get("name")) && !TextUtils.isEmpty(map.get("phone"))) {
                list.add(map);
            }
        }
        contractCursor.close();
        return list;
    }

}
